package org.example.big_data.service.impl;

import org.example.big_data.mapper.UserMapper;
import org.example.big_data.pojo.User;
import org.example.big_data.service.UserService;
import org.example.big_data.utils.Md5Util;
import org.example.big_data.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        //controller层传过来的密码password需要加密处理
        String md5String = Md5Util.getMD5String(password);
        userMapper.add(username,md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());  //更新一下”更新时间“字段
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //更改用户头像是需要用到当前登录用户的id，所以需要从ThreadLocal中获取（工具类提供get方法）
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);  //将sql所需的id值通过传参的方式传入mapper
    }

    @Override
    public void updatePwd(String newPwd) {
        //sql需要用户id值，先获取id值，传参传入mapper中的方法，加密后存入
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        String md5String = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(md5String,id);
    }


}
